// ImageRetrieval.cpp : implementation file
//

#include "stdafx.h"
#include "ImageLab.h"
#include "ImageRetrieval.h"
#include "ChildFrm.h"
#include "Output.h"


// CImageRetrieval dialog

IMPLEMENT_DYNAMIC(CImageRetrieval, CDialog)

CImageRetrieval::CImageRetrieval(CWnd* pParent /*=NULL*/)
	: CDialog(CImageRetrieval::IDD, pParent)
	, m_RetrievalNum(0)
{
	Is_TextShow = 0;
	ASSERT(pParent != NULL);
	m_Parent = pParent;
	m_nID = CImageRetrieval::IDD;
}

CImageRetrieval::~CImageRetrieval()
{
	CImageRetrieval::SendMessage(WM_CLOSE);
}

void CImageRetrieval::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

//create the dialog
BOOL CImageRetrieval::Create()
{
	BOOL isCreated = CDialog::Create(m_nID, m_Parent);
	if(isCreated == TRUE)
	{
		//create the dialog for displaying text
		m_DisplayText = new CDisplayText(this);
		if(m_DisplayText->Create()==FALSE)
		{
			CImageRetrieval::DestroyWindow();
			::AfxMessageBox(CString("Failed to Create Dialog for Displaying Text"), MB_OK, 0);
			return FALSE;
		}
		RECT lpRect;
		m_DisplayText->GetWindowRect(&lpRect);
		m_DisplayText->MoveWindow(0, 0, lpRect.right-lpRect.left, lpRect.bottom-lpRect.top, 1);
		CImageRetrieval::InitializeRetrieval();
	}
	return isCreated;
}

//post destroy the window
void CImageRetrieval::PostNcDestroy()
{
	//delete the pointer in parent
	m_DisplayText->DestroyWindow();
	if(m_Parent)
		((CChildFrame*)m_Parent)->m_ImageRetrieval = NULL;
	CImageRetrieval::DestroyWindow();
	return;
}

//initialize the retrieval dialog
void CImageRetrieval::InitializeRetrieval()
{
	//get the pointer of the Queries Edit
	CEdit* m_Query1 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY1);
	CEdit* m_Query2 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY2);
	CEdit* m_Query3 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY3);
	//clear the content
	m_Query1->SetWindowTextW(CString(""));
	m_Query2->SetWindowTextW(CString(""));
	m_Query3->SetWindowTextW(CString(""));

	//Get the pointer of the Filed Control
	CComboBox* m_Field1 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD1);
	CComboBox* m_Field2 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD2);
	CComboBox* m_Field3 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD3);
	//Set the Initialize Selection to AllFields
	m_Field1->SelectString(-1, CString("Caption"));
	m_Field2->SelectString(-1, CString("Caption"));
	m_Field3->SelectString(-1, CString("Caption"));

	//Get the pointer of the AndOr Control
	CComboBox* m_AndOr1 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ANDOR1);
	CComboBox* m_AndOr2 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ANDOR2);
	//Set to the Default Selection of And
	m_AndOr1->SelectString(-1, CString("And"));
	m_AndOr2->SelectString(-1, CString("And"));

	//Get the Pointer of the Sort Control
	CComboBox* m_Order1 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER1);
	CComboBox* m_Order2 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER2);
	CComboBox* m_Order3 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER3);
	//Set to the Default of Name
	m_Order1->SelectString(-1, CString("Name"));
	m_Order2->SelectString(-1, CString("Caption"));
	m_Order3->SelectString(-1, CString("Reference"));

	//Get the pointer to the sort control
	CComboBox* m_Sort1 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING1);
	CComboBox* m_Sort2 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING2);
	CComboBox* m_Sort3 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING3);
	//Set to the Defaul of Descending
	m_Sort1->SelectString(-1, CString("Descending"));
	m_Sort2->SelectString(-1, CString("Descending"));
	m_Sort3->SelectString(-1, CString("Descending"));

	//Get the Pointer to the Image List
	CListBox* m_ImageList = (CListBox*)CImageRetrieval::GetDlgItem(IDC_IMAGELIST);
	//Clear the content
	m_ImageList->ResetContent();

	m_RetrievalNum = 0;
	CImageRetrieval::UpdateRetrievalNum();

	//initialize the display text
	m_DisplayText->InitializeText();

	return;
}

BEGIN_MESSAGE_MAP(CImageRetrieval, CDialog)
	ON_BN_CLICKED(IDC_CLEARRESULTS, &CImageRetrieval::OnBnClickedClearresults)
	ON_BN_CLICKED(IDC_RETRIEVE, &CImageRetrieval::OnBnClickedRetrieve)
	ON_LBN_SELCHANGE(IDC_IMAGELIST, &CImageRetrieval::OnLbnSelchangeImagelist)
	ON_BN_CLICKED(IDC_SHOWTEXT, &CImageRetrieval::OnBnClickedShowtext)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CImageRetrieval message handlers

void CImageRetrieval::OnBnClickedClearresults()
{
	// TODO: Add your control notification handler code here
	//clear all the entry in the dialog
	CImageRetrieval::InitializeRetrieval();
	return;
}

void CImageRetrieval::OnBnClickedRetrieve()
{
	// TODO: Add your control notification handler code here

	//Open the database
	m_RetrievalNum = 0;
	m_ImageIndex.m_strFilter = "";
	m_ImageIndex.m_strSort = "";
	try
	{
		m_ImageIndex.Open();
	}
	catch(CDBException *e)
	{
		e->ReportError();
		return;
	}
	//initialize the display text
	m_DisplayText->InitializeText();

	//Set the order
	m_ImageIndex.m_strSort += CImageRetrieval::OnGetFirstOrder();
	m_ImageIndex.m_strSort += ",";
	m_ImageIndex.m_strSort += CImageRetrieval::OnGetSecondOrder();
	m_ImageIndex.m_strSort += ",";
	m_ImageIndex.m_strSort += CImageRetrieval::OnGetThirdOrder();

	//requery the database
	try
	{
		m_ImageIndex.Requery();
	}
	catch(CException *e)
	{
		e->ReportError();
		return;
	}

	//Get the ListBox
	CListBox* m_ImageList = (CListBox*)CImageRetrieval::GetDlgItem(IDC_IMAGELIST);
	if(m_ImageList->GetCount()!=0)
		m_ImageList->ResetContent();
	//Display the record
	while(!m_ImageIndex.IsEOF())
	{
		//validate if the record satisfy the Queires
		//Check with the first Query
		//Get the first query
		bool isTrue1;
		CEdit* m_Query1 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY1);
		if(m_Query1->GetWindowTextLengthW()<=3&&m_Query1->GetWindowTextLengthW()>=1)
		{
			::AfxMessageBox(CString("First Query Less than 3 Characters"), MB_OK, 0);
			return;
		}
		//check with the first query
		if(m_Query1->GetWindowTextLengthW()==0)
			isTrue1 = 1;
		else
			isTrue1 = CImageRetrieval::OnValidateFirst();

		//Get the second query
		bool isTrue2;
		CEdit* m_Query2 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY2);
		if(m_Query2->GetWindowTextLengthW()<=3&&m_Query2->GetWindowTextLengthW()>=1)
		{
			::AfxMessageBox(CString("Second Query Less than 3 Characters"), MB_OK, 0);
			return;
		}
		//check with the first query
		if(m_Query2->GetWindowTextLengthW()==0)
			isTrue2 = 1;
		else
			isTrue2 = CImageRetrieval::OnValidateSecond();

		//Get the third query
		bool isTrue3;
		CEdit* m_Query3 = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY3);
		if(m_Query3->GetWindowTextLengthW()<=3&&m_Query3->GetWindowTextLengthW()>=1)
		{
			::AfxMessageBox(CString("Third Query Less than 3 Characters"), MB_OK, 0);
			return;
		}
		//check with the first query
		if(m_Query3->GetWindowTextLengthW()==0)
			isTrue3 = 1;
		else
			isTrue3 = CImageRetrieval::OnValidateThird();

		//combine the status of the three Queries
		bool isTrue;
		//get the ANDOR results of ANDOR1 and ANDOR2
		CComboBox* m_AndOr1 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ANDOR1);
		CComboBox* m_AndOr2 = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ANDOR2);
		//get the and or status
		if(m_AndOr1->GetWindowTextLengthW()==3)		//AND is selecte
			isTrue = isTrue1&&isTrue2;
		else										//OR is selected
			isTrue = isTrue1||isTrue2;
		if(m_AndOr2->GetWindowTextLengthW()==3)		//AND is selected
			isTrue = isTrue&&isTrue3;
		else										//OR is selected
			isTrue = isTrue||isTrue3;
		if(isTrue)
		{
			//true, add the image to the list
			CString m_CurName = CString(m_ImageIndex.m_ImageName);
			m_ImageList->AddString(m_CurName);
			m_RetrievalNum ++;
		}
		//move to the next record
		m_ImageIndex.MoveNext();
	}
	CImageRetrieval::UpdateRetrievalNum();
	m_ImageIndex.Close();
	return;
}

//validate the data record with the first query
bool CImageRetrieval::OnValidateFirst(void)
{
	CEdit* m_Query = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY1);
	bool isTrue = 0;

	//Get the text
	LPTSTR m_Text = new WCHAR[256];
	int m_QueryLen = m_Query->GetLine(0, m_Text);
	//convert the WCHAR to char
	char* m_TextChar = (char*)malloc(sizeof(char)*m_QueryLen+1);
	size_t m_ConvertedChars = 0;
	wcstombs_s(&m_ConvertedChars, m_TextChar, m_QueryLen+1, m_Text, m_QueryLen);

	//Get the Selected Field
	CComboBox* m_Field = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD1);
	//get the selected string
	LPTSTR m_FieldText = new WCHAR[256];
	m_Field->GetWindowText(m_FieldText, m_Field->GetWindowTextLengthW());

	//validate if the char exist in the captions or not
	//retreive the caption
	switch(m_Field->FindString(-1, CString(m_FieldText, m_Field->GetWindowTextLengthW())))
	{
	case 0:		//search on image name
		{
			char* m_ImageName = m_ImageIndex.m_ImageName.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_ImageName, 0, m_QueryLen, (int)m_ImageIndex.m_ImageName.GetLength())
				<m_ImageIndex.m_ImageName.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 1:		//search on captions
		{
			char* m_Caption = m_ImageIndex.m_Caption.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Caption, 0, m_QueryLen, (int)m_ImageIndex.m_Caption.GetLength())
				<m_ImageIndex.m_Caption.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 2:		//search on references
		{
			char* m_Reference = m_ImageIndex.m_Reference.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Reference, 0, m_QueryLen, (int)m_ImageIndex.m_Reference.GetLength())
				<m_ImageIndex.m_Reference.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 3:		//search on titles
		{
			char* m_Title = m_ImageIndex.m_Title.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Title, 0, m_QueryLen, (int)m_ImageIndex.m_Title.GetLength())
				<m_ImageIndex.m_Title.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 4:		//search on authors
		{
			char* m_Authors = m_ImageIndex.m_Authors.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Authors, 0, m_QueryLen, (int)m_ImageIndex.m_Authors.GetLength())
				<m_ImageIndex.m_Authors.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 5:		//search on fulltext
		{
			char* m_Fulltext1 = m_ImageIndex.m_Fulltext1.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext1.GetLength())
				<m_ImageIndex.m_Fulltext1.GetLength()-m_QueryLen-1)
				isTrue = 1;
			if(!isTrue)
			{
				char* m_Fulltext2 = m_ImageIndex.m_Fulltext2.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext2, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext2.GetLength())
					<m_ImageIndex.m_Fulltext2.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext3 = m_ImageIndex.m_Fulltext3.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext3.GetLength())
					<m_ImageIndex.m_Fulltext3.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext4 = m_ImageIndex.m_Fulltext4.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext4.GetLength())
					<m_ImageIndex.m_Fulltext4.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			break;
		}
	}
	//free memory

	//return results
	return isTrue;
}

//validate the second query
bool CImageRetrieval::OnValidateSecond(void)
{
	CEdit* m_Query = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY2);
	bool isTrue = 0;

	//Get the text
	LPTSTR m_Text = new WCHAR[256];
	int m_QueryLen = m_Query->GetLine(0, m_Text);
	//convert the WCHAR to char
	char* m_TextChar = (char*)malloc(sizeof(char)*m_QueryLen+1);
	size_t m_ConvertedChars = 0;
	wcstombs_s(&m_ConvertedChars, m_TextChar, m_QueryLen+1, m_Text, m_QueryLen);

	//Get the Selected Field
	CComboBox* m_Field = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD2);
	//get the selected string
	LPTSTR m_FieldText = new WCHAR[256];
	m_Field->GetWindowText(m_FieldText, m_Field->GetWindowTextLengthW());

	//validate if the char exist in the captions or not
	//retreive the caption
	switch(m_Field->FindString(-1, CString(m_FieldText, m_Field->GetWindowTextLengthW())))
	{
	case 0:		//search on image name
		{
			char* m_ImageName = m_ImageIndex.m_ImageName.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_ImageName, 0, m_QueryLen, (int)m_ImageIndex.m_ImageName.GetLength())
				<m_ImageIndex.m_ImageName.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 1:		//search on captions
		{
			char* m_Caption = m_ImageIndex.m_Caption.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Caption, 0, m_QueryLen, (int)m_ImageIndex.m_Caption.GetLength())
				<m_ImageIndex.m_Caption.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 2:		//search on references
		{
			char* m_Reference = m_ImageIndex.m_Reference.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Reference, 0, m_QueryLen, (int)m_ImageIndex.m_Reference.GetLength())
				<m_ImageIndex.m_Reference.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 3:		//search on titles
		{
			char* m_Title = m_ImageIndex.m_Title.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Title, 0, m_QueryLen, (int)m_ImageIndex.m_Title.GetLength())
				<m_ImageIndex.m_Title.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 4:		//search on authors
		{
			char* m_Authors = m_ImageIndex.m_Authors.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Authors, 0, m_QueryLen, (int)m_ImageIndex.m_Authors.GetLength())
				<m_ImageIndex.m_Authors.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 5:		//search on fulltext
		{
			char* m_Fulltext1 = m_ImageIndex.m_Fulltext1.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext1.GetLength())
				<m_ImageIndex.m_Fulltext1.GetLength()-m_QueryLen-1)
				isTrue = 1;
			if(!isTrue)
			{
				char* m_Fulltext2 = m_ImageIndex.m_Fulltext2.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext2, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext2.GetLength())
					<m_ImageIndex.m_Fulltext2.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext3 = m_ImageIndex.m_Fulltext3.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext3.GetLength())
					<m_ImageIndex.m_Fulltext3.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext4 = m_ImageIndex.m_Fulltext4.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext4.GetLength())
					<m_ImageIndex.m_Fulltext4.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			break;
		}
	}
	//free memory

	//return results
	return isTrue;
}

//validate the third query
bool CImageRetrieval::OnValidateThird(void)
{
	CEdit* m_Query = (CEdit*)CImageRetrieval::GetDlgItem(IDC_QUERY3);
	bool isTrue = 0;

	//Get the text
	LPTSTR m_Text = new WCHAR[256];
	int m_QueryLen = m_Query->GetLine(0, m_Text);
	//convert the WCHAR to char
	char* m_TextChar = (char*)malloc(sizeof(char)*m_QueryLen+1);
	size_t m_ConvertedChars = 0;
	wcstombs_s(&m_ConvertedChars, m_TextChar, m_QueryLen+1, m_Text, m_QueryLen);

	//Get the Selected Field
	CComboBox* m_Field = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_FIELD3);
	//get the selected string
	LPTSTR m_FieldText = new WCHAR[256];
	m_Field->GetWindowText(m_FieldText, m_Field->GetWindowTextLengthW());

	//validate if the char exist in the captions or not
	//retreive the caption
	switch(m_Field->FindString(-1, CString(m_FieldText, m_Field->GetWindowTextLengthW())))
	{
	case 0:		//search on image name
		{
			char* m_ImageName = m_ImageIndex.m_ImageName.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_ImageName, 0, m_QueryLen, (int)m_ImageIndex.m_ImageName.GetLength())
				<m_ImageIndex.m_ImageName.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 1:		//search on captions
		{
			char* m_Caption = m_ImageIndex.m_Caption.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Caption, 0, m_QueryLen, (int)m_ImageIndex.m_Caption.GetLength())
				<m_ImageIndex.m_Caption.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 2:		//search on references
		{
			char* m_Reference = m_ImageIndex.m_Reference.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Reference, 0, m_QueryLen, (int)m_ImageIndex.m_Reference.GetLength())
				<m_ImageIndex.m_Reference.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 3:		//search on titles
		{
			char* m_Title = m_ImageIndex.m_Title.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Title, 0, m_QueryLen, (int)m_ImageIndex.m_Title.GetLength())
				<m_ImageIndex.m_Title.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 4:		//search on authors
		{
			char* m_Authors = m_ImageIndex.m_Authors.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Authors, 0, m_QueryLen, (int)m_ImageIndex.m_Authors.GetLength())
				<m_ImageIndex.m_Authors.GetLength()-m_QueryLen-1)
				isTrue = 1;
			break;
		}
	case 5:		//search on fulltext
		{
			char* m_Fulltext1 = m_ImageIndex.m_Fulltext1.GetBuffer();
			if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext1.GetLength())
				<m_ImageIndex.m_Fulltext1.GetLength()-m_QueryLen-1)
				isTrue = 1;
			if(!isTrue)
			{
				char* m_Fulltext2 = m_ImageIndex.m_Fulltext2.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext2, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext2.GetLength())
					<m_ImageIndex.m_Fulltext2.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext3 = m_ImageIndex.m_Fulltext3.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext3.GetLength())
					<m_ImageIndex.m_Fulltext3.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			if(!isTrue)
			{
				char* m_Fulltext4 = m_ImageIndex.m_Fulltext4.GetBuffer();
				if(bf.SearchStr(m_TextChar, m_Fulltext1, 0, m_QueryLen, (int)m_ImageIndex.m_Fulltext4.GetLength())
					<m_ImageIndex.m_Fulltext4.GetLength()-m_QueryLen-1)
					isTrue = 1;
			}
			break;
		}
	}
	//free memory

	//return results
	return isTrue;
}

void CImageRetrieval::OnLbnSelchangeImagelist()
{
	//retrieval the data based on the filename
	CListBox* m_ImageList = (CListBox*)CImageRetrieval::GetDlgItem(IDC_IMAGELIST);
	//get the index of the current selected item
	int m_CurSelected = m_ImageList->GetCurSel();
	//get the name of the image selected
	CString m_ImageName;
	m_ImageList->GetText(m_CurSelected, m_ImageName);

	//open the database
	m_ImageData.Open();
	//requery the database
	m_ImageData.m_strFilter = CString("[ImageName] = '");
	m_ImageData.m_strFilter += m_ImageName;
	m_ImageData.m_strFilter += CString("'");
	m_ImageData.Requery();

	//save the image into c:\temp.bmp and load it
	if(!m_ImageData.IsEOF())
	{
		//open the file
		CFile cfile;
		CFileException e;
		try
		{
			cfile.Open(CString("c:\\temp.bmp"), CFile::modeCreate|CFile::modeWrite, &e);
			//lock the memory
			LPSTR buffer = (LPSTR)GlobalLock(m_ImageData.m_ImageContent.m_hData);
			//write the memory to file
			cfile.Write(buffer, (UINT)m_ImageData.m_ImageContent.m_dwDataLength);
		}
		catch(CException *e)		//catch and display the errors
		{
			e->ReportError();
			return;
		}
		cfile.Close();
		//show the text
		m_DisplayText->OnSetCaption(CString(m_ImageData.m_Caption));	//caption
		m_DisplayText->OnSetReference(CString(m_ImageData.m_Reference));	//reference
		m_DisplayText->OnSetAuthors(CString(m_ImageData.m_Authors));	//authors
		CString m_Fulltext = CString(m_ImageData.m_Fulltext1);
		m_Fulltext += CString(m_ImageData.m_Fulltext2);
		m_Fulltext += CString(m_ImageData.m_Fulltext3);
		m_Fulltext += CString(m_ImageData.m_Fulltext4);
		m_DisplayText->OnSetFulltext(CString(m_Fulltext));	//fulltext
	}
	else
	{
		::AfxMessageBox(CString("Cannot find the image!"), MB_OK, 0);
		return;
	}
	m_ImageData.Close();

	//get the partent and load the image to CChildView
	((CChildFrame*)m_Parent)->m_wndView.OnImageOpen(CString("c:\\temp.bmp"), CString("temp.bmp"));

	return;
}

//update the number of retrieved images
void CImageRetrieval::UpdateRetrievalNum(void)
{
	//get the poiter to the edit contral
	CEdit* m_edit = (CEdit*)CImageRetrieval::GetDlgItem(IDC_RETRIEVALNUMBER);
	//convert the int to string
	CString str = bf.douTostr(m_RetrievalNum);
	//truncate the zeros after the point
	str.Truncate((int)log10((double)(max(m_RetrievalNum, 1)))+1);
	//set the string to windows
	m_edit->SetWindowTextW((LPCTSTR)str);
	return;
}

//show or hide text
void CImageRetrieval::OnBnClickedShowtext()
{
	CButton* m_ShowText = (CButton*)CImageRetrieval::GetDlgItem(IDC_SHOWTEXT);
	//show the window
	if(!Is_TextShow)
	{
		m_DisplayText->ShowWindow(SW_SHOW);
		m_ShowText->SetWindowTextW(CString("HideText"));
	}
	else	//hide the window
	{
		m_DisplayText->ShowWindow(SW_HIDE);
		m_ShowText->SetWindowTextW(CString("ShowText"));
	}
	Is_TextShow = !Is_TextShow;
	// TODO: Add your control notification handler code here
}

//close the dialog
afx_msg void CImageRetrieval::OnClose()
{
	m_DisplayText->DestroyWindow();
	//delete the pointer in parent
	if(m_Parent)
		((CChildFrame*)m_Parent)->m_ImageRetrieval = NULL;
	CImageRetrieval::DestroyWindow();
	return;
}

//get the order of the retrieval
CString CImageRetrieval::OnGetFirstOrder(void)
{
	CString m_SelectedOrder;
	//get the poiters to the first selection
	CComboBox* m_Order = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER1);
	CComboBox* m_AscDesc = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING1);

	//get the selection of column
	WCHAR m_Selection[256];
	int m_Selected;
	m_Order->GetWindowTextW(m_Selection, m_Order->GetWindowTextLengthW());
	m_Selected = m_Order->FindString(-1, CString(m_Selection, m_Order->GetWindowTextLengthW()));
	switch(m_Selected)
	{
	case 0:
		m_SelectedOrder += "[ImageName]";
		break;
	case 1:
		m_SelectedOrder += "[Caption]";
		break;
	case 2:
		m_SelectedOrder += "[Reference]";
		break;
	case 3:
		m_SelectedOrder += "[Title]";
		break;
	case 4:
		m_SelectedOrder += "[Author]";
		break;
	case 5:
		m_SelectedOrder += "[Fulltext1]";
		break;
	default:
		break;
	}

	//get the selection of descending or ascending
	m_AscDesc->GetWindowTextW(m_Selection, m_AscDesc->GetWindowTextLengthW());
	m_Selected = m_AscDesc->FindString(-1, CString(m_Selection, m_AscDesc->GetWindowTextLengthW()));
	if(m_Selected == 0)
		m_SelectedOrder += "DESC";
	else
		m_SelectedOrder += "ASC";

	return m_SelectedOrder;
}

//get the order of the second selection
CString CImageRetrieval::OnGetSecondOrder(void)
{
	CString m_SelectedOrder;
	//get the poiters to the first selection
	CComboBox* m_Order = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER2);
	CComboBox* m_AscDesc = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING2);

	//get the selection of column
	WCHAR m_Selection[256];
	int m_Selected;
	m_Order->GetWindowTextW(m_Selection, m_Order->GetWindowTextLengthW());
	m_Selected = m_Order->FindString(-1, CString(m_Selection, m_Order->GetWindowTextLengthW()));
	switch(m_Selected)
	{
	case 0:
		m_SelectedOrder += "[ImageName]";
		break;
	case 1:
		m_SelectedOrder += "[Caption]";
		break;
	case 2:
		m_SelectedOrder += "[Reference]";
		break;
	case 3:
		m_SelectedOrder += "[Title]";
		break;
	case 4:
		m_SelectedOrder += "[Author]";
		break;
	case 5:
		m_SelectedOrder += "[Fulltext1]";
		break;
	default:
		break;
	}

	//get the selection of descending or ascending
	m_AscDesc->GetWindowTextW(m_Selection, m_AscDesc->GetWindowTextLengthW());
	m_Selected = m_AscDesc->FindString(-1, CString(m_Selection, m_AscDesc->GetWindowTextLengthW()));
	if(m_Selected == 0)
		m_SelectedOrder += "DESC";
	else
		m_SelectedOrder += "ASC";

	return m_SelectedOrder;
}

//get the order of the third selection
CString CImageRetrieval::OnGetThirdOrder(void)
{
	CString m_SelectedOrder;
	//get the poiters to the first selection
	CComboBox* m_Order = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ORDER3);
	CComboBox* m_AscDesc = (CComboBox*)CImageRetrieval::GetDlgItem(IDC_ASCENDINGDESCENDING3);

	//get the selection of column
	WCHAR m_Selection[256];
	int m_Selected;
	m_Order->GetWindowTextW(m_Selection, m_Order->GetWindowTextLengthW());
	m_Selected = m_Order->FindString(-1, CString(m_Selection, m_Order->GetWindowTextLengthW()));
	switch(m_Selected)
	{
	case 0:
		m_SelectedOrder += "[ImageName]";
		break;
	case 1:
		m_SelectedOrder += "[Caption]";
		break;
	case 2:
		m_SelectedOrder += "[Reference]";
		break;
	case 3:
		m_SelectedOrder += "[Title]";
		break;
	case 4:
		m_SelectedOrder += "[Author]";
		break;
	case 5:
		m_SelectedOrder += "[Fulltext1]";
		break;
	default:
		break;
	}

	//get the selection of descending or ascending
	m_AscDesc->GetWindowTextW(m_Selection, m_AscDesc->GetWindowTextLengthW());
	m_Selected = m_AscDesc->FindString(-1, CString(m_Selection, m_AscDesc->GetWindowTextLengthW()));
	if(m_Selected == 0)
		m_SelectedOrder += "DESC";
	else
		m_SelectedOrder += "ASC";

	return m_SelectedOrder;
}

//On Press Enter
void CImageRetrieval::OnOK()
{
	CImageRetrieval::OnBnClickedRetrieve();
	return;
}
